<template>
  <div class="container">
    <Breadcrumb :items="['勾兑', '原酒产量']" />
    <a-card direction="vertical" :size="12" fill title="产量管理">
      <BlendSearch ref="blendSearch" :getWineProInf="getWineProInf" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="12">
          <a-space>
            <a-button type="primary" @click="openBlendDialog">
              <template #icon>
                <icon-plus />
              </template>
              新增原酒
            </a-button>
            <a-tooltip :content="$t('searchTable.actions.refresh')">
              <div class="action-icon" @click="getWineProInf()"
                ><icon-refresh size="18"
              /></div>
            </a-tooltip>
          </a-space>
        </a-col>
      </a-row>
      <a-table
        :columns="columns"
        :data="tableData"
        :bordered="{ wrapper: true, cell: true }"
      >
        <template #oneTotal="{ record }">
          {{ getOneTotal(record) }}
        </template>
        <template #twoTotal="{ record }">
          {{ getTwoTotal(record) }}
        </template>
        <template #dayTotal="{ record }">
          {{ getDayTotal(record) }}
        </template>
        <template #oneLinearTotal="{ record }">
          {{ getOneLinearTotal(record) }}
        </template>
        <template #twoLinearTotal="{ record }">
          {{ getTwoLinearTotal(record) }}
        </template>
      </a-table>
    </a-card>
    <BlendDialog
      ref="blendDialog"
      :groupList="groupList"
      :getWineProInf="getWineProInf"
      :tankList="tankList"
    />
  </div>
</template>

<script lang="ts" setup>
import { nextTick, ref } from 'vue';
import BlendSearch from './components/blendSearch.vue';
import BlendDialog from './components/blendDialog.vue';
import { getGroupInfo } from '@/api/produce';
import { Message } from '@arco-design/web-vue';
import { getWinePro, getWineTankList } from '@/api/winepro';
const tableData = ref([]);
const blendDialog = ref(null);
const blendSearch = ref(null);
const openBlendDialog = () => {
  blendDialog.value.openDialog();
};
const winePro = ref([]); // 当月产量数据
const tankList = ref([]); // 酒罐数据
// 分组车间数据
const groupList = ref([]);
const getGroupList = async () => {
  let params = {
    workshop_id: 1,
  };
  const { data, code } = await getGroupInfo(params);
  if (code == 200) {
    groupList.value = data;
  }
  let newData = [];
  groupList.value.forEach((item) => {
    newData.push({
      title: item.group_alias,
      children: [
        {
          title: '大楂',
          align: 'center',
          dataIndex: `one_cha${item.group_id}`,
          width: 80,
        },
        {
          title: '酒度',
          align: 'center',
          dataIndex: `one_linear${item.group_id}`,
          width: 80,
        },
        {
          title: '二楂',
          align: 'center',
          dataIndex: `two_cha${item.group_id}`,
          width: 80,
        },
        {
          title: '酒度',
          align: 'center',
          dataIndex: `two_linear${item.group_id}`,
          width: 80,
        },
      ],
    });
  });
  columns.value.splice(1, 0, ...newData);
};
getGroupList();
const getAllDay = () => {
  let params = blendSearch.value.form;

  const today = new Date(params.search_pro_date);
  const year = today.getFullYear();
  const month = today.getMonth();
  // 获取当前月份的第一天和最后一天
  const firstDay = new Date(year, month, 1);
  const lastDay = new Date(year, month + 1, 0);
  const days = [];
  const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
  // 生成所有日期对象
  tableData.value = [];
  for (let date = firstDay; date <= lastDay; date.setDate(date.getDate() + 1)) {
    const day = new Date(date);
    days.push({
      date: `${day.getMonth() + 1}月${day.getDate()}日`,
      weekday: weekdays[day.getDay()],
    });
    tableData.value.push({
      day: `${day.getMonth() + 1}月${day.getDate()}日`,
    });
  }
};
const getWineProInf = async () => {
  getAllDay();
  let params = blendSearch.value.form;
  const { data, code } = await getWinePro(params);
  if (code == 200) {
    Message.success('获取成功');
    const year = data[0]?.pro_date?.split('-')[0] || '2025';
    tableData.value.forEach((item) => {
      let newDay = null;
      const match = item.day.match(/(\d{1,2})月(\d{1,2})日/);
      let month = match[1].padStart(2, '0'); // 补零到两位数
      let day = match[2].padStart(2, '0'); // 补零到两位数
      newDay = `${year}-${month}-${day}`; // 替换为实际需要的年份
      let newData = data.filter((entity) => entity.pro_date == newDay);
      if (newData.length > 0) {
        groupList.value.forEach((gorup) => {
          let groupInfo = newData.filter(
            (dataInfo) => dataInfo.group_id == gorup.group_id
          );
          if (groupInfo) {
            let oneInfo = groupInfo.find((info) => info.types == 1);
            if (oneInfo) {
              item[`one_cha${gorup.group_id}`] = oneInfo.zhe_six_five_du;
              item[`one_linear${gorup.group_id}`] = oneInfo.zhe_he_du;
            } else {
              item[`one_cha${gorup.group_id}`] = 0;
              item[`one_linear${gorup.group_id}`] = 0;
            }
            let twoInfo = groupInfo.find((info) => info.types == 2);
            if (twoInfo) {
              item[`two_cha${gorup.group_id}`] = twoInfo.zhe_six_five_du;
              item[`two_linear${gorup.group_id}`] = twoInfo.zhe_he_du;
            } else {
              item[`two_cha${gorup.group_id}`] = 0;
              item[`two_linear${gorup.group_id}`] = 0;
            }
          } else {
            item[`one_cha${gorup.group_id}`] = 0;
            item[`one_linear${gorup.group_id}`] = 0;
            item[`two_cha${gorup.group_id}`] = 0;
            item[`two_linear${gorup.group_id}`] = 0;
          }
        });
      } else {
        groupList.value.forEach((gorup) => {
          item[`one_cha${gorup.group_id}`] = 0;
          item[`one_linear${gorup.group_id}`] = 0;
          item[`two_cha${gorup.group_id}`] = 0;
          item[`two_linear${gorup.group_id}`] = 0;
        });
      }
    });
  }
};
nextTick(() => {
  setTimeout(() => {
    getWineProInf();
  }, 300);
});

const gettankList = async () => {
  const { code, data } = await getWineTankList();
  if (code === 200) {
    tankList.value = data;
  }
};
gettankList()
const columns = ref([
  { title: '日期', align: 'center', dataIndex: 'day', width: 120 },
  {
    title: '大楂小计',
    align: 'center',
    width: 120,
    fixed: 'right',
    slotName: 'oneTotal',
  },
  {
    title: '二楂小计',
    align: 'center',
    width: 120,
    fixed: 'right',
    slotName: 'twoTotal',
  },
  {
    title: '每日共计',
    align: 'center',
    width: 120,
    fixed: 'right',
    slotName: 'dayTotal',
  },
  {
    title: '大楂每日平均酒度',
    align: 'center',
    width: 120,
    slotName: 'oneLinearTotal',
  },
  {
    title: '二楂每日平均酒度',
    align: 'center',
    width: 120,
    slotName: 'twoLinearTotal',
  },
]);
// 获取大X小计
const getOneTotal = (record) => {
  let total = 0;
  groupList.value.forEach((item) => {
    if (record[`one_cha${item.group_id}`] != undefined) {
      total += record[`one_cha${item.group_id}`] * 1;
    }
  });
  return total;
};
// 获取二X小计
const getTwoTotal = (record) => {
  let total = 0;
  groupList.value.forEach((item) => {
    if (record[`two_cha${item.group_id}`] != undefined) {
      total += record[`two_cha${item.group_id}`] * 1;
    }
  });
  return total;
};
// 获取每日共计
const getDayTotal = (record) => {
  let total = 0;
  groupList.value.forEach((item) => {
    if (record[`one_cha${item.group_id}`] != undefined) {
      total += record[`one_cha${item.group_id}`] * 1;
    }
    if (record[`two_cha${item.group_id}`] != undefined) {
      total += record[`two_cha${item.group_id}`] * 1;
    }
  });
  return total;
};
// 获取大X每日平均酒度
const getOneLinearTotal = (record) => {
  let total = 0;
  groupList.value.forEach((item) => {
    if (record[`one_linear${item.group_id}`] != undefined) {
      total += record[`one_linear${item.group_id}`] * 1;
    }
  });
  return (total / groupList.value.length).toFixed(1);
};
// 获取二X每日平均酒度
const getTwoLinearTotal = (record) => {
  let total = 0;
  groupList.value.forEach((item) => {
    if (record[`two_linear${item.group_id}`] != undefined) {
      total += record[`two_linear${item.group_id}`] * 1;
    }
  });
  return (total / groupList.value.length).toFixed(1);
};
</script>

<style lang="scss" scoped>
</style>